﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VehicleOOBWeb.Filters;
using VehicleOOBWeb.Models;

namespace VehicleOOBWeb.Controllers
{
    [Authorize]
    [InitializeSimpleMembership]
    public class OrderController : Controller
    {
        //
        // GET: /Order/
        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /Order/Details/5
        public ActionResult Details(int id)
        {
            return View();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult AddToCart(int id)
        {
            List<Cart> lstCart = null;
            if (Session["CartList"] == null)
            {
                lstCart = new List<Cart>();
                lstCart.Add(new Cart()
                {
                    item_id = id.ToString(),
                    item_quantity = 1
                });
            }
            else
            {
                lstCart = Session["CartList"] as List<Cart>;
                if (lstCart != null)
                {
                    if (lstCart.Count(c => c.item_id == id.ToString()) == 0)
                    {
                        lstCart.Add(new Cart()
                        {
                            item_id = id.ToString(),
                            item_quantity = 1
                        });
                    }
                }
            }
            Session["CartList"] = lstCart;
            return this.Json(true);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="removeIds"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult RemoveOutCart(string removeIds)
        {
            string[] arrRemoveID = removeIds.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            List<Cart> lstCart = Session["CartList"] as List<Cart>;
            if (lstCart != null && arrRemoveID != null)
            {
                lstCart.RemoveAll(c => arrRemoveID.Contains(c.item_id.ToString()));
                Session["CartList"] = lstCart;
            }
            return this.Json(true);
        }

        /// <summary>
        /// Thực hiện xem giỏ hàng
        /// </summary>
        /// <returns></returns>
        public ActionResult ViewCarts()
        {
            try
            {
                if (Session["CartList"] != null)
                {
                    List<Cart> lstCart = Session["CartList"] as List<Cart>;
                    for (int i = 0; i < lstCart.Count; i++)
                    {
                        Vehicle vehicle = VehicleService.GetVehicleByID(int.Parse(lstCart[i].item_id));
                        if (vehicle != null)
                        {
                            lstCart[i].item_manufacturer = vehicle.Manufacturer.manufacturer_name;
                            lstCart[i].item_name = vehicle.name;
                            lstCart[i].item_price = vehicle.price.ToString();
                        }
                    }
                    return View(lstCart);
                }
                else
                {
                    return View();
                }
            }
            catch (Exception)
            {
                return Redirect("Views/Shared/Error");
            }
        }

        //
        // GET: /Order/Create
        public ActionResult Create()
        {
            if (Session["CartList"] != null)
            {
                List<Cart> lstCart = Session["CartList"] as List<Cart>;
                for (int i = 0; i < lstCart.Count; i++)
                {
                    Vehicle vehicle = VehicleService.GetVehicleByID(int.Parse(lstCart[i].item_id));
                    if (vehicle != null)
                    {
                        lstCart[i].item_manufacturer = vehicle.Manufacturer.manufacturer_name;
                        lstCart[i].item_name = vehicle.name;
                        lstCart[i].item_price = vehicle.price.ToString();
                    }
                }
                ViewBag.Carts = lstCart;

            }
            else
            {
                ViewBag.Carts = null;
            }
            ViewBag.PersonalInfo = PersonInfoService.GetPersonalInfo(User.Identity.Name);
            return View();
        }

        //
        // POST: /Order/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                if (Session["CartList"] != null)
                {
                    List<Cart> lstCart = Session["CartList"] as List<Cart>;
                    Order order = new Order();
                    UpdateModel(order);
                    if (lstCart != null)
                    {
                        order.OrderDetails = new System.Data.Linq.EntitySet<OrderDetail>();
                        for (int i = 0; i < lstCart.Count; i++)
                        {
                            OrderDetail orderDetail = new OrderDetail();
                            orderDetail.quantity = lstCart[i].item_quantity;
                            orderDetail.vehicle_id = int.Parse(lstCart[i].item_id);
                            order.OrderDetails.Add(orderDetail);
                        }
                    }
                    OrderService.AddOrder(order);
                }
                else
                {

                }

                return View("OrderSuccess");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Order/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Order/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Order/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Order/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
